/**
 * 
 */
package hammersmith.local.ui.gui;

import hammersmith.local.valueobjects.Article;
import hammersmith.local.valueobjects.Customer;
import hammersmith.local.valueobjects.Invoice;
import hammersmith.local.valueobjects.ShoppingCart;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

/**
 * @author hammersmith
 *
 */
public class InvoiceMask extends JFrame {

	private static final long serialVersionUID = -8439581492798653778L;

	public InvoiceMask(Invoice invoice, Customer customer){
		this.setSize(727,658);
		this.setLocationRelativeTo(null);
		getContentPane().setLayout(null);
		this.setResizable(false);
		
		JPanel pnlHeader = new JPanel();
		pnlHeader.setBounds(6, 6, 715, 114);
		getContentPane().add(pnlHeader);
		pnlHeader.setLayout(null);
		
		JLabel lblRechnung = new JLabel("Rechnung");
		lblRechnung.setFont(new Font("Dialog", Font.BOLD, 16));
		lblRechnung.setHorizontalAlignment(SwingConstants.CENTER);
		lblRechnung.setBounds(6, 6, 703, 16);
		pnlHeader.add(lblRechnung);
		
		JLabel lblName = new JLabel("Name: "+customer.getLastname()+", "+customer.getFirstname());
		lblName.setBounds(6, 34, 351, 16);
		pnlHeader.add(lblName);
		
		JLabel lblAdresse = new JLabel("Adresse: "+customer.getStreet());
		lblAdresse.setBounds(6, 62, 351, 16);
		pnlHeader.add(lblAdresse);
		
		JLabel lblInvoiceNumber = new JLabel("RechnungsNr.: "+invoice.getInvoiceNumber());
		lblInvoiceNumber.setBounds(6, 90, 351, 16);
		pnlHeader.add(lblInvoiceNumber);
		
		JLabel lblId = new JLabel("KundenNr.: "+customer.getCustomerId());
		lblId.setBounds(361, 34, 348, 16);
		pnlHeader.add(lblId);
		
		JLabel lblDate = new JLabel("Datum: "+invoice.getDate());
		lblDate.setBounds(361, 90, 348, 16);
		pnlHeader.add(lblDate);
		
		JLabel lblPlzort = new JLabel("PLZ/Ort: "+customer.getZip()+"/"+customer.getCity());
		lblPlzort.setBounds(361, 62, 61, 16);
		pnlHeader.add(lblPlzort);
		
		JPanel pnlContent = new JPanel();
		pnlContent.setBounds(6, 132, 715, 415);
		getContentPane().add(pnlContent);
		
		JButton btnPrint = new JButton("Drucken");
		btnPrint.setBounds(604, 586, 117, 29);
		getContentPane().add(btnPrint);
		
		JButton btnOk = new JButton("Ok");
		btnOk.setBounds(542, 586, 66, 29);
		getContentPane().add(btnOk);
		btnOk.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
			
				dispose();
				
			}
		});
		
		JScrollPane scrollPane = new JScrollPane();
		final JTable table = new JTable();
		table.setAutoCreateRowSorter(true);

		DefaultTableModel model = new DefaultTableModel() {

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		model.addColumn("Art.-Nr.");
		model.addColumn("Typ");
		model.addColumn("Name");
		model.addColumn("Größe");
		model.addColumn("Farbe");
		model.addColumn("Preis");
		model.addColumn("Anzahl");

		ShoppingCart cart = invoice.getCart();

		Hashtable<Article, Integer> list = cart.getList();
		for (Article article : list.keySet()) {

			model.addRow(new Object[] { String.valueOf(article.getId()),
					article.getType(), article.getName(), article.getSize(),
					article.getColor(), String.valueOf(article.getPrice()),
					list.get(article).intValue() });

		}

		table.setModel(model);
		
		table.getColumnModel().getColumn(0).setResizable(false);
		table.getColumnModel().getColumn(0).setPreferredWidth(58);
		table.getColumnModel().getColumn(1).setResizable(false);
		table.getColumnModel().getColumn(1).setPreferredWidth(100);
		table.getColumnModel().getColumn(2).setResizable(false);
		table.getColumnModel().getColumn(2).setPreferredWidth(165);
		table.getColumnModel().getColumn(3).setResizable(false);
		table.getColumnModel().getColumn(3).setPreferredWidth(60);
		table.getColumnModel().getColumn(4).setResizable(false);
		table.getColumnModel().getColumn(4).setPreferredWidth(83);
		table.getColumnModel().getColumn(5).setResizable(false);
		table.getColumnModel().getColumn(5).setPreferredWidth(50);
		table.getColumnModel().getColumn(6).setResizable(false);
		table.getColumnModel().getColumn(6).setPreferredWidth(54);
		
		pnlContent.add(scrollPane);
		scrollPane.setViewportView(table);
		
		JLabel lblGesamtpreis = new JLabel("Gesamtpreis: "+cart.getShoppingCartFullCosts()+" €");
		lblGesamtpreis.setBounds(542, 559, 179, 16);
		getContentPane().add(lblGesamtpreis);
		
		this.setVisible(true);
		
	}
}
